package cn.huobi.org.service.impl;

import cn.huobi.basic.service.BaseService;
import cn.huobi.basic.service.impl.BaseServiceImpl;
import cn.huobi.basic.util.PageList;
import cn.huobi.org.domain.Department;
import cn.huobi.org.domain.Employee;
import cn.huobi.org.mapper.DepartmentMapper;
import cn.huobi.org.query.DepartmentQuery;
import cn.huobi.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    /**
     * 部门联级查询，简单写法
     */
//    @Override
//    public List<Department> deptTree() {
//        // 查询出所有部门
//        List<Department> allDepts = departmentMapper.findAll();
//        // 创建空数组，存放遍历出来的部门
//        List <Department> depts = new ArrayList<>();
//        for (Department dept : allDepts) {
//            // 判断该部门是否有上级部门，没有直接添加进数组
//            if (dept.getParentId() == null){
//                depts.add(dept);
//            }else {
//                // 如果该部门还有上级部门，再次遍历找它的上级部门
//                for (Department parent : allDepts) {
//                    // 判断当前的部门的上级部门的id与再遍历出来的部门id比较
//                    if (dept.getParentId().equals(parent.getId())){
//                        List<Department> children = parent.getChildren();
//                        children.add(dept);
//                        break;
//                    }
//                }
//            }
//        }
//
//        return depts;
//    }

    /**
     * stream流写法
     * @return
     */
    @Override
    public List<Department> deptTree() {
        // 创建空数组，存放遍历出来的部门
        List <Department> depts = new ArrayList<>();
        // 查询出所有部门
        List<Department> allDepts = departmentMapper.findAll();
        if(CollectionUtils.isEmpty(allDepts)){
            return depts;
        }
        // stream
        Map<Long,Department> parentMap = allDepts.stream().collect(Collectors.toMap(Department::getId,e -> e));
        // 等同于
//        Map<Long, Department> parentMap = new HashMap<>();
//        for (Department dept : allDepts) {
//            parentMap.put(dept.getId(),dept);
//        }
        for (Department dept : allDepts) {
            // 判断该部门是否有上级部门，没有直接添加进数组
            if (dept.getParentId() == null){
                depts.add(dept);
            }else {
                Department parent = parentMap.get(dept.getParentId());
                if (parent == null){
                   continue;
                }
                List<Department> children = parent.getChildren();
                children.add(dept);
            }
        }
        return depts;
    }
}
